package com.hx.advice;


import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.beans.factory.config.BeanPostProcessor;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

import java.util.Arrays;
import java.util.List;
@Component
@Aspect  //代表这是一个切面类
@Order(2) //后执行
public class LogAdvice {


    private String name="hello";

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    @Pointcut("execution(* com.hx.service.Impl.*.*(..))")
    public void pt() {}
    @Before("pt()")
    public  void beforeLog(JoinPoint joinPoint ){
        //可以通连接点获取相应的目标信息
        String methodName = joinPoint.getSignature().getName();
        Object[] args = joinPoint.getArgs();
        List<Object> params=args == null ? null : Arrays.asList(args);
        System.out.println(methodName+"方法开始执行了----参数"+params);
    }

    @AfterReturning(value = "pt()" ,returning = "result")
    protected void commit(JoinPoint joinPoint,Object result){
        String methodName = joinPoint.getSignature().getName();
        Object[] args = joinPoint.getArgs();
        List<Object> params=args == null ? null : Arrays.asList(args);
        System.out.println(methodName+"准备提交事务了----结果:"+result+"参数:"+params);
    }


}
